%{

#include <iostream>

#include "main.hpp"

extern void yyerror( char* errorString );
extern int yylineno;

%}

whitespace    [ \t]+
delimiter     [\[\]\(\);:,]
digit         [0-9]
letter        [a-zA-Z]
assignop      ":="
exprop       "*"|"+"|"|"
relop         "="|"<"|">"
integer       {digit}+
identifier    {letter}({letter}|{digit})*
character     "’"."’"
boolean       "true"|"false"

%x IN_COMMENT

%%

\n                          { yylineno++; }

"program"                   return PROGRAM;

"begin"                     return BEG;
"end"                       return END;

"type"                      return TYPE;
"integer"                   return INTEGER;
"boolean"                   return BOOLEAN;
"char"                      return CHAR;
"value"                     return VALUE;

"procedure"                 return PROCEDURE;

"if"                        return IF;
"then"                      return THEN;
"else"                      return ELSE;
"endif"                     return ENDIF;

"while"                     return WHILE;
"do"                        return DO;
"endwhile"                  return ENDWHILE;

"read"                      return READ;
"write"                     return WRITE;

"not"                       return NOT;

{whitespace}                ;

{delimiter}                 return *yytext;

{assignop}                  return ASSIGNOP;

{exprop}                    return *yytext;
{relop}                     return *yytext;

{integer}                   return INT_LITERAL;
{character}                 return CHAR_LITERAL;
{boolean}                   return BOOL_LITERAL;

{identifier}                return IDENTIFIER;

.                           yyerror( "Invalid character." );

"/*"                        BEGIN(IN_COMMENT);

<IN_COMMENT>
{
     "*/"      BEGIN(INITIAL);
     [^*\n]+   ; // ignore everything inside the comments
     "*"       ; // ignore the lone star
     \n        yylineno++;
}


%%

void yyerror( char* str )
{
  printf( "%s : %d\n", str, yylineno );
}

int yywrap()
{
  return 1;
}
